//
//  CatSettingCell.m
//  EquesCat
//
//  Created by Chun Hung Cheung on 2024/12/20.
//

#import "CatSettingCell.h"
@interface CatSettingCell()<UIScrollViewDelegate>
@property(nonatomic,strong)UILabel *titleLabel;
@property(nonatomic,strong)UILabel *titleSubLabel;

@property(nonatomic,strong)UIImageView *arrowicon;
@property(nonatomic,strong)UILabel *subTitleLabel;
@property(nonatomic,strong)UISwitch *switchButton;
@property(nonatomic,strong)UIButton *updateButton;
@property(nonatomic,strong)UIButton *deleteButton;

@property(nonatomic,strong)UILabel *wifi_risi_label;
@property(nonatomic,strong)UIView *coreview;
@property(nonatomic,strong)UIImageView *wifiImage;
@property(nonatomic,strong)UILabel *WifiLbel;
@property(nonatomic,strong)UIView *surfaceView;


@end

@implementation CatSettingCell

- (void)awakeFromNib {
    [super awakeFromNib];

}


- (UIImageView*)arrowicon
{
    if (!_arrowicon) {
        _arrowicon = [[UIImageView alloc]init];
        [self.contentView addSubview:_arrowicon];
        [_arrowicon mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.width.mas_equalTo(7);
            make.height.mas_equalTo(12);
            make.centerY.equalTo(self.contentView.mas_centerY);
            make.right.equalTo(self.contentView.mas_right).offset(-14);
        }];
    }
    return _arrowicon;
}

- (UILabel*)titleLabel
{
    if (!_titleLabel) {
         _titleLabel = [[UILabel alloc]init];
        [self.contentView addSubview:_titleLabel];
         _titleLabel.textColor = [HexChangeRGB colorWithHexString:@"#333333"];
         _titleLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:16.0];
         _titleLabel.numberOfLines = 0;
         _titleLabel.textAlignment = NSTextAlignmentLeft;
        [_titleLabel sizeToFit];
        [_titleLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.contentView.mas_left).offset(14);
            make.centerY.equalTo(self.contentView.mas_centerY);
        }];
   }
    return _titleLabel;
}


- (UILabel*)titleSubLabel
{
    if (!_titleSubLabel) {
        _titleSubLabel = [[UILabel alloc]init];
        [self.contentView addSubview:_titleSubLabel];
         _titleSubLabel.textColor = [HexChangeRGB colorWithHexString:@"#808080"];
         _titleSubLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:13.0];
         _titleSubLabel.numberOfLines = 0;
         _titleSubLabel.textAlignment = NSTextAlignmentLeft;
        [_titleSubLabel sizeToFit];
        [_titleSubLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.contentView.mas_left).offset(14);
            make.right.equalTo(self.contentView.mas_right).offset(-68);
            make.top.equalTo(self.titleLabel.mas_bottom);
        }];
   }
    return _titleSubLabel;
}


- (UILabel*)subTitleLabel
{
    if (!_subTitleLabel) {
         _subTitleLabel = [[UILabel alloc]init];
         [self.contentView addSubview:_subTitleLabel];
         _subTitleLabel.textColor = [HexChangeRGB colorWithHexString:@"#666666"];
         _subTitleLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:14.0];
         _subTitleLabel.numberOfLines = 0;
         _subTitleLabel.textAlignment = NSTextAlignmentLeft;
        [_subTitleLabel sizeToFit];
        [_subTitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self.contentView.mas_right).offset(-30);
            make.centerY.equalTo(self.contentView.mas_centerY);
        }];
   }
    return _subTitleLabel;
}

- (UIButton*)updateButton
{
    if (!_updateButton) {
        //1、添加底层层的电话和删除按钮
         _updateButton = [[UIButton alloc] init];
        [self.contentView addSubview:_updateButton];
        [_updateButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.mas_equalTo(160);
            make.height.mas_equalTo(25);
            make.right.equalTo(self.contentView.mas_right).offset(-14);
            make.centerY.equalTo(self.contentView.mas_centerY);
        }];
        [_updateButton setTitleColor:[HexChangeRGB colorWithHexString:@"#666666"] forState:UIControlStateNormal];
         _updateButton.titleLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:14.0];
        //绑定打电话事件
        [_updateButton addTarget:self action:@selector(updateButtonClick) forControlEvents:UIControlEventTouchUpInside];
    }
     return _updateButton;
}

- (void)updateButtonClick
{
    if (self.updateBlock) {
        self.updateBlock(self.model);
    }
}


- (UISwitch*)switchButton
{
     if (!_switchButton) {
         _switchButton = [[UISwitch alloc]init];
         _switchButton.transform = CGAffineTransformMakeScale(0.8, 0.8);
         _switchButton.onTintColor = [UIColor orangeColor];
        [_switchButton addTarget:self action:@selector(switchButtonClick:) forControlEvents:UIControlEventValueChanged];
        [self.contentView addSubview:_switchButton];
        [_switchButton mas_makeConstraints:^(MASConstraintMaker *make) {
           make.centerY.equalTo(self.contentView.mas_centerY);
           make.right.equalTo(self.contentView.mas_right).offset(-14);
        }];
    }
    return _switchButton;
}





- (void)switchButtonClick:(UISwitch*)sender;
{
    if (_statusBlock) {
        _statusBlock(sender.isOn,self.model);
    }
}



- (UIButton*)deleteButton
{
    if (!_deleteButton) {
         _deleteButton = [[UIButton alloc]init];
         _deleteButton.backgroundColor = [UIColor whiteColor];
        [_deleteButton setTitle:NSLocalizedString(@"remove_equipment_settings", nil) forState:UIControlStateNormal];
         _deleteButton.titleLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:18.0];
        [_deleteButton setTitleColor:[HexChangeRGB colorWithHexString:@"#FF7316"] forState:UIControlStateNormal];
        [_deleteButton addTarget:self action:@selector(deleteButtonClick) forControlEvents:UIControlEventTouchUpInside];
        [self.contentView addSubview:_deleteButton];
        [_deleteButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(self.contentView.mas_centerY);
            make.height.mas_equalTo(44);
            make.width.mas_equalTo(260);
            make.centerX.equalTo(self.contentView.mas_centerX);
        }];
        _deleteButton.layer.cornerRadius = 22;
        _deleteButton.layer.masksToBounds = YES;
        _deleteButton.layer.borderWidth = 1.0;
        _deleteButton.layer.borderColor = [HexChangeRGB colorWithHexString:@"#FF7316"].CGColor;
    }
    return _deleteButton;
}

- (void)deleteButtonClick
{
    if(self.deleteBlock){
        self.deleteBlock(self.model);
    }
}

/*蒙版*/
- (UIView *)surfaceView{
    if (nil == _surfaceView) {
         _surfaceView = [[UIView alloc]init];
         _surfaceView.alpha = 0.6;
         _surfaceView.backgroundColor = WXHColor(242, 242, 242, 1.0);
        [self.contentView addSubview:_surfaceView];
        [_surfaceView mas_makeConstraints:^(MASConstraintMaker *make){
            make.edges.mas_equalTo(self);
            make.centerY.equalTo(self.mas_centerY);
        }];
    }
    return _surfaceView;
}



- (void)setModel:(CatSettingModel *)model
{
    _model = model;
    
    switch (model.type) {
        case 0:
        {
            [self remoove3];
            [self.titleLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(self.contentView.mas_left).offset(14);
                make.centerY.equalTo(self.contentView.mas_centerY);
            }];
             self.titleLabel.text = _model.title;
             self.arrowicon.image = [UIImage imageNamed:@"voice_v2_arrow_next"];
            
            if (_model.subTitle.length > 1) {
                self.subTitleLabel.text = _model.subTitle;
            }else{
                if (_subTitleLabel) {
                    [_subTitleLabel removeFromSuperview];
                    _subTitleLabel = nil;
                }
            }
            /*
            if (model.device_status == 0) {
                self.surfaceView.hidden = NO;
            }else{
                self.surfaceView.hidden = YES;
            }
             */
        }
            break;
        case 1:
        {
            [self remoove1];
            [self remoove3];
             self.titleLabel.text = _model.title;
            if (_model.subTitle.length > 1) {
                 self.titleLabel.text = _model.title;
                [self.titleLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
                    make.left.equalTo(self.contentView.mas_left).offset(14);
                    make.top.equalTo(self.contentView.mas_top).offset(4);
                }];
                 self.titleSubLabel.text = _model.subTitle;
               
            }else{
                 self.titleLabel.text = _model.title;
                [self.titleLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
                    make.left.equalTo(self.contentView.mas_left).offset(14);
                    make.centerY.equalTo(self.contentView.mas_centerY);
                }];
            }
             self.switchButton.on = _model.status;
            
             self.surfaceView.hidden = YES;
            
        }
            break;
        case 2:
        {
            [self remoove1];
            [self remoove2];
            [self.deleteButton setTitle:NSLocalizedString(@"删除设备", nil) forState:UIControlStateNormal];
        }
            break;
            
        case 3:
        {
            [self remoove1];
            [self remoove2];
            [self remoove3];
             self.titleLabel.text = _model.title;
             self.subTitleLabel.text = _model.subTitle;
            
        }
            break;
        case 4:
        {
        
            [self remoove1];
            [self remoove2];
            [self remoove3];
             self.titleLabel.text = _model.title;
            [self.updateButton setTitle:_model.subTitle  forState:UIControlStateNormal];
            if ([_model.subTitle isEqualToString:NSLocalizedString(@"点击升级", nil)]) {
                [self.updateButton setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
                 self.updateButton.backgroundColor = [HexChangeRGB colorWithHexString:@"#D9D0C1"];
                 self.updateButton.layer.cornerRadius = 12;
                 self.updateButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
            }else{
                [self.updateButton setTitleColor:[HexChangeRGB colorWithHexString:@"#666666"] forState:UIControlStateNormal];
                 self.updateButton.backgroundColor = [UIColor clearColor];
                 self.updateButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
            }
            
            self.surfaceView.hidden = YES;
            
        }
            break;
            
        case 5:
        {
        
            [self remoove1];
            [self remoove2];
            [self remoove3];
             self.titleLabel.text = _model.title;

            [self.WifiLbel mas_remakeConstraints:^(MASConstraintMaker *make){
                make.centerY.equalTo(self.contentView.mas_centerY);
                make.right.mas_equalTo(self.contentView.mas_right).offset(-94);
            }];
            self.WifiLbel.text = _model.wifi_name;
             
            switch ([_model.subTitle intValue]) {
                case 0:
                    self.wifiImage.image = [UIImage imageNamed:@"wifi0"];
                    break;
                case 1:
                    self.wifiImage.image = [UIImage imageNamed:@"wifi1"];
                    break;
                case 2:
                    self.wifiImage.image = [UIImage imageNamed:@"wifi2"];
                    break;
                case 3:
                    self.wifiImage.image = [UIImage imageNamed:@"wifi3"];
                    break;
                case 4:
                    self.wifiImage.image = [UIImage imageNamed:@"wifi4"];
                    break;
                default:
                    self.wifiImage.image = [UIImage imageNamed:@"wifi2"];
                    break;
            }
            
            [self.wifiImage mas_remakeConstraints:^(MASConstraintMaker *make){
                make.centerY.equalTo(self.contentView.mas_centerY);
                make.width.mas_equalTo(22);
                make.height.mas_equalTo(16);
                if(_model.wifi_name){
                    make.left.equalTo(self.WifiLbel.mas_right).offset(4);
                }else{
                    make.right.equalTo(self.contentView.mas_right).offset(-60);
                }
            }];
            
            self.wifi_risi_label.text = [NSString stringWithFormat:@"%@dbm",_model.wifi_config];
            
        }
            break;
            
        case 6:
        {
            [self remoove1];
            [self remoove2];
            [self remoove3];
             self.titleLabel.text = _model.title;
             self.subTitleLabel.text = _model.subTitle;
             self.arrowicon.image = [UIImage imageNamed:@"voice_v2_arrow_next"];
        }
            break;
            
    

            
        default:
            break;
    }
}



- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

}

- (void)remoove1
{
    if(_arrowicon){
     [_arrowicon removeFromSuperview];
      _arrowicon = nil;
    }
    if(_subTitleLabel){
     [_subTitleLabel removeFromSuperview];
      _subTitleLabel = nil;
    }
    if(_updateButton){
      [_updateButton removeFromSuperview];
        _updateButton = nil;
    }
    
    if (_coreview) {
        [_coreview removeFromSuperview];
        _coreview = nil;
    }
    
    if (_wifiImage) {
        [_wifiImage removeFromSuperview];
        _wifiImage = nil;
    }
    if (_WifiLbel) {
       [_WifiLbel removeFromSuperview];
        _WifiLbel = nil;
    }
    
    if (_surfaceView) {
       [_surfaceView removeFromSuperview];
        _surfaceView = nil;
    }

    
}


- (void)remoove2
{
    if(_subTitleLabel){
     [_subTitleLabel removeFromSuperview];
      _subTitleLabel = nil;
     }
    
    if(_titleLabel){
     [_titleLabel removeFromSuperview];
      _titleLabel = nil;
    }
    if(_updateButton){
      [_updateButton removeFromSuperview];
        _updateButton = nil;
    }
    
    if (_coreview) {
        [_coreview removeFromSuperview];
        _coreview = nil;
    }
    
    if (_wifiImage) {
        [_wifiImage removeFromSuperview];
        _wifiImage = nil;
    }
    if (_WifiLbel) {
       [_WifiLbel removeFromSuperview];
        _WifiLbel = nil;
    }
    if (_surfaceView) {
       [_surfaceView removeFromSuperview];
        _surfaceView = nil;
    }

}

- (void)remoove3
{
    if(_deleteButton){
      [_deleteButton removeFromSuperview];
       _deleteButton = nil;
    }
    
    if(_switchButton){
      [_switchButton removeFromSuperview];
       _switchButton = nil;
    }
    
    if(_updateButton){
      [_updateButton removeFromSuperview];
        _updateButton = nil;
    }
    
    if (_coreview) {
        [_coreview removeFromSuperview];
        _coreview = nil;
    }
    
    if (_wifiImage) {
        [_wifiImage removeFromSuperview];
        _wifiImage = nil;
    }
    if (_WifiLbel) {
       [_WifiLbel removeFromSuperview];
        _WifiLbel = nil;
    }
    if (_surfaceView) {
       [_surfaceView removeFromSuperview];
        _surfaceView = nil;
    }
    
    if (_subTitleLabel) {
        [_subTitleLabel removeFromSuperview];
        _subTitleLabel = nil;
    }
    
     if (_titleSubLabel) {
        [_titleSubLabel removeFromSuperview];
        _titleSubLabel = nil;
    }

}


/*db*/
- (UILabel*)wifi_risi_label{
    if (nil == _wifi_risi_label) {
        _coreview = [[UIView alloc]init];
        _coreview.layer.cornerRadius = 4;
        _coreview.layer.borderWidth = 0.8;
        _coreview.layer.borderColor = [HexChangeRGB colorWithHexString:@"#999999"].CGColor;
        _coreview.layer.masksToBounds = YES;
        [self.contentView addSubview:_coreview];
        [_coreview mas_makeConstraints:^(MASConstraintMaker *make){
            make.centerY.equalTo(self.contentView.mas_centerY);
            make.right.equalTo(self.contentView.mas_right).offset(-14);
            make.width.mas_equalTo(47);
            make.height.mas_equalTo(16);
        }];
         _wifi_risi_label = [[UILabel alloc] init];
         _wifi_risi_label.textColor = [HexChangeRGB colorWithHexString:@"#999999"];
        [_wifi_risi_label setFont:[UIFont fontWithName:@"PingFangSC-Regular" size:10.0]];
        [_wifi_risi_label sizeToFit];
        [_coreview addSubview:_wifi_risi_label];
        [_wifi_risi_label mas_makeConstraints:^(MASConstraintMaker *make){
            make.centerY.equalTo(_coreview.mas_centerY);
            make.centerX.equalTo(_coreview.mas_centerX);
        }];
    }
    return _wifi_risi_label;
}

/*网络图标*/
- (UIImageView*)wifiImage
{
    if (nil == _wifiImage) {
         _wifiImage = [[UIImageView alloc]init];
         _wifiImage.contentMode = UIViewContentModeCenter;
        [self.contentView addSubview:_wifiImage];
        [_wifiImage mas_makeConstraints:^(MASConstraintMaker *make){
            make.centerY.equalTo(self.contentView.mas_centerY);
            make.width.mas_equalTo(22);
            make.height.mas_equalTo(16);
            make.left.mas_equalTo(self.WifiLbel.mas_right).offset(4);
        }];
    }
  return _wifiImage;
}

/*网络名称*/
- (UILabel*)WifiLbel{
    if (nil == _WifiLbel) {
         _WifiLbel = [[UILabel alloc] init];
         _WifiLbel.textColor = [HexChangeRGB colorWithHexString:@"#7f7f7f"];
        [_WifiLbel setFont:[UIFont fontWithName:@"PingFangSC-Regular" size:16.0]];
        [_WifiLbel sizeToFit];
        [self.contentView addSubview:_WifiLbel];
        [_WifiLbel mas_makeConstraints:^(MASConstraintMaker *make){
            make.centerY.equalTo(self.contentView.mas_centerY);
            make.right.mas_equalTo(self.contentView.mas_right).offset(-54);
        }];
    }
    return _WifiLbel;
}


- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer {
    if ([gestureRecognizer isKindOfClass:[UIPanGestureRecognizer class]]) {
        // 如果 panGestureRecognizer 是滑动手势，返回 YES，让它能够处理手势
        return YES;
    }
    return NO;
}

@end
